<?php
class ServerSvc
{/*{{{*/
	const OBJ = 'Server';
	static public function add($param)
	{
		$obj = Server::createByBiz($param);
		return self::getDao()->add($obj);
	}
	static public function getById($id = '0')
	{
		if (empty($id))
		{
			return null;
		}
		return self::getDao()->getById($id,self::OBJ);
	}

	static public function updateById($id,$param)
	{
		return self::getDao()->updateById($id,$param,self::OBJ);
	}

	static private function getDao()
	{
		return LoaderSvc::loadDao(self::OBJ);
	}

	static public function lists($request = array(),$options = array(),$export = false)
	{/*{{{*/
		$request_param = array();
		$sql_condition = array();
		$sql_param = array();

		if(isset($request['id']) && $request['id']>1)
		{
			$request_param[] = '`id`=' . $request['id'];
			$sql_condition[] = '`id` = ? ';
			$sql_param[] = $request['id'];
		}
		if('' != $request['sgid']){
			$request_param[] = 'sgid=' . $request['sgid'];
			$sql_condition[] = '`sgid` = ?';
			$sql_param[]	 = $request['sgid'];
		}
		if('' != $request['ip']){
			$request_param[] = 'ip=' . $request['ip'];
			$sql_condition[] = '`ip` = ?';
			$sql_param[]	 = $request['ip'];
		}
		if('' != $request['spath']){
			$request_param[] = 'spath=' . $request['spath'];
			$sql_condition[] = '`spath` = ?';
			$sql_param[]	 = $request['spath'];
		}
		if('' != $request['stype']){
			$request_param[] = 'stype=' . $request['stype'];
			$sql_condition[] = '`stype` = ?';
			$sql_param[]	 = $request['stype'];
		}
		if('' != $request['port']){
			$request_param[] = 'port=' . $request['port'];
			$sql_condition[] = '`port` = ?';
			$sql_param[]	 = $request['port'];
		}
		if('' != $request['sname']){
			$request_param[] = 'sname=' . $request['sname'];
			$sql_condition[] = '`sname` = ?';
			$sql_param[]	 = $request['sname'];
		}
		if('' != $request['spass']){
			$request_param[] = 'spass=' . $request['spass'];
			$sql_condition[] = '`spass` = ?';
			$sql_param[]	 = $request['spass'];
		}
		if('' != $request['edition']){
			$request_param[] = 'edition=' . $request['edition'];
			$sql_condition[] = '`edition` = ?';
			$sql_param[]	 = $request['edition'];
		}
		if('' != $request['status']){
			$request_param[] = 'status=' . $request['status'];
			$sql_condition[] = '`status` = ?';
			$sql_param[]	 = $request['status'];
		}
        if($request['startime'])
        {
            $request_param[] = 'startime='.$request['startime'];
            $sql_condition[] = 'utime >= ?';
            $sql_param[] = $request['startime'];
        }
        if($request['endtime'])
        {
            $request_param[] = 'endtime='.$request['endtime'];
            $sql_condition[] = 'utime<=?';
            $sql_param[] = $request['endtime'];

        }

		$option = array();
		$option['len'] = ($options['len'] > 0) ? $options['len'] : PER_PAGE;
		if($options['page'] > 0){
			$option['offset'] = ($options['page'] - 1) * $option['len'];
		}
		$option['orderby'] = isset($options['orderby']) ? $options['orderby'] : '';
		$results = self::getDao()->getRecord($sql_condition,$sql_param ,$option);
		$pages = '';
		$total = $results['total'];
		if($total > 0){
			$temp = stristr($options['baseurl'],'?');
			if($temp === false) $options['baseurl'] .= '?';
			$options['baseurl'] .= implode('&',$request_param);
			if(count($request_param)) $options['baseurl'] .= '&';
			$pages = Pager::getPageStr($options['page'],$option['len'],$total,$options['baseurl']);
		}
		$results['pages'] = $pages;
		//$results['offset'] = $option['offset'] + 1;
		//$results['len'] = $option['len'];
		$results['pagenums'] = ceil($total / $option['len']);

		return $results;
	}/*}}}*/
	
	static public function delRecordById($id)
	{
		self::getDao()->delRecordById($id);
	}

}